package edu.zju.gis.dbfg.model.util;

import edu.zju.gis.dbfg.common.util.FileUtil;
import edu.zju.gis.dbfg.common.util.StringUtils;

import java.io.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author Chenyr
 * @date 2019/10/22
 * 按需处理WKT文件
 **/
public class WKTProcess {

    public static void main(String[] args) throws IOException {
        String input = args[0];
        String outputDir = args[1];
        if(FileUtil.exist(outputDir))
            FileUtil.deletePath(outputDir);
        File outdir = new File(outputDir);
        outdir.mkdirs();
        File inDir = new File(input);
        File files[] = inDir.listFiles();
        for(File f:files){
            deleteFField(f,outputDir);
        }
        System.out.println("输出结束。");
    }

    //删除特定字段 index:66 name:ff
    public static boolean deleteFField(File inputFile, String outputDir ){
        try {
            ArrayList<String> strArray = new ArrayList<String>();
            InputStreamReader reader = new InputStreamReader(new FileInputStream(inputFile));
            BufferedReader br = new BufferedReader(reader);
            String line = "";
            line = br.readLine();
            while(line != null){
                String field[] = line.split("\t");
                List<String> fieldList = Arrays.asList(field);
                List<String> list = new ArrayList<String>(fieldList);
                list.remove(66);
                strArray.add(StringUtils.join(list.toArray(),"\t"));
                //读取新的line
                line = br.readLine();
//                break;
            }

            FileUtil.write(outputDir+File.separator+inputFile.getName(), strArray);
//            File outputFile = new File(outputDir+File.separator+inputFile.getName());
//            FileWriter fw = new FileWriter(outputFile);
//            for(String str:strArray) {
//                fw.append(str).append("\n").flush();
//            }
//            fw.close();
            return true;
        }
        catch (Exception ex){

            return false;
        }
    }
}
